#!/usr/bin/env python
"""
Illustrate simple contour plotting, contours on an image with
a colorbar for the contours, and labelled contours.

See also contour_image.py.
"""
from __future__ import division
from matplotlib.patches import Patch
from pylab import *
import os

start_quantity_of_files = 0;
end_quantity_of_files = input("Enter quantity of files: ")

gas_index = 1;

directory = "result " + str(gas_index) + "/pressure"
if not os.path.exists(directory):
    os.makedirs(directory)    

f_info = file("../output/gas %i/info%i.txt"%(gas_index, start_quantity_of_files),"r")
f_info.readline()
f_info.readline()
f_info.readline()
f_info.readline()
f_info.readline()
GridSize_x = int(f_info.readline().rsplit()[3])
GridSize_y = int(f_info.readline().rsplit()[3])
print "Grid:", GridSize_x, GridSize_y

FLAG_use_contour = 1
FLAG_use_interpolation = 1
quantity_of_files = start_quantity_of_files
start_file_name = "pressure"
start_info_file_name = "info"
global_data = np.loadtxt("../output/gas %i/global_info.txt"%(gas_index))
min_concentration = global_data[0]
max_concentration = global_data[1]
min_temperature = global_data[2]
max_temperature = global_data[3]
min_mod_gas_velocity = global_data[4]
max_mod_gas_velocity = global_data[5]
min_pressure = global_data[6]
max_pressure = global_data[7]
max_quantity_of_contours_conc = 20
max_quantity_of_contours_temp = 20

FLAG_only_temperature = 0


#sp hack??
sp = GridSize_x
GridSize_x = GridSize_y
GridSize_y = sp

while(quantity_of_files < end_quantity_of_files and not(FLAG_only_temperature)):
    #file_name = "%s %d\t%3.2f %-8s" % ("Values:",42,56.89732,"tot")
    input_file_name = "%s%i%s%s%i.txt" % ("../output/gas ", gas_index, "/", start_file_name, quantity_of_files)
    data = np.loadtxt(input_file_name)
    
    #data begining with concentration
    quantity_of_contours = max_quantity_of_contours_conc
    
    #print(quantity_of_contours)
    NX=GridSize_x;  
    NY=GridSize_y;
    X = data[:,0].reshape(NX,NY)
    Y = data[:,1].reshape(NX,NY)
    Z = data[:,2].reshape(NX,NY)

    for i in range(0, NX):
        for j in range(0, NY):
            if Z[i,j] == 0:
                Z[i,j] = np.nan    

    #plt.figure()
    #plt.figure(figsize=(34.7/2.54, 27.75/2.54)) # in santimeters
    plt.figure(figsize=(34.7/2.54, (NX+50)/NY*27.75/2.54)) # in santimeters                    
                   

    im = imshow(Z)
    #im.set_interpolation('bilinear')
    if( not FLAG_use_interpolation ):
        im.set_interpolation('none')
    im.set_clim(min_pressure, max_pressure)    
    #pcolor(X, Y, Z, norm=plt.Normalize(0.200, 0.25))
    #pcolor(X, Y, Z)
    #colorbar()
    colorbar(orientation='horizontal')
    if( ( quantity_of_contours > 0 ) & FLAG_use_contour ):
        CS = plt.contour(X, Y, Z, quantity_of_contours,
            colors='k', # negative contours will be dashed by default
            )
        plt.clabel(CS, inline=1, fontsize=10)
    ax = plt.axis([0, NY-1, 0, NX-1]) #hack again??

    """
    plt.figure()
    CS = plt.contour(X, Y, Z)
    plt.clabel(CS, inline=1, fontsize=10)
    """
    f_info = file("../output/gas %i/info%i.txt"%(gas_index,quantity_of_files),"r")
    f_info.readline()
    f_info.readline()
    f_info.readline()
    quantity_of_iterations = f_info.readline().rsplit()[4]
    f_info.readline()
    f_info.readline()
    f_info.readline()    
    time_step = f_info.readline().rsplit()[2]    
           
    
    str_title = "Gas %i  Pressure   t = %.2lf" % (gas_index,int(quantity_of_iterations)*float(time_step));
    plt.title(str_title)    
    #plt.show()
    output_file_name = "%s%s%i.png" % ("result " + str(gas_index) + "/Pressure/", \
                                       start_file_name, quantity_of_files)
    plt.savefig(output_file_name)
    print 100*float(quantity_of_files)/end_quantity_of_files, "%"
    quantity_of_files = quantity_of_files + 1
    plt.close()
print "Done."    
#input("Done.")
